﻿using AttachScreenShotToJira.CODE.CORE_LOGIC;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace AttachScreenShotToJira.FORMS
{
    public partial class frmSettings : Form
    {
        public frmSettings()
        {
            InitializeComponent();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void frmSettings_Load(object sender, EventArgs e)
        {
            //load the settings
            edxJiraServer.Text = Properties.Settings.Default.sJiraServer;
            edxUsername.Text = Properties.Settings.Default.sUsername;
            edxPassword.Text = Properties.Settings.Default.sPassword;

            //display greenshot ini settings
            if (Properties.Settings.Default.sGreenshotINI == "")
            {
                //try and get default
                edxINI.Text = GREENSHOT_HANDLING.getGreenShotPathIni();
            }
            else
            {
                edxINI.Text = Properties.Settings.Default.sGreenshotINI;
                //handle view
                btnGreenShot.Enabled = !GREENSHOT_HANDLING.AlreadyInGreenshot();
            }

            btnGreenShot.Enabled = !GREENSHOT_HANDLING.AlreadyInGreenshot();            
        }

        private bool validateForm()
        {
            if (edxJiraServer.Text == "")
            {
                MessageBox.Show("Please provide the servername.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                edxJiraServer.Focus();
                return false;
            }

            if (edxUsername.Text == "")
            {
                MessageBox.Show("Please provide your username.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                edxUsername.Focus();
                return false;
            }

            if (edxPassword.Text == "")
            {
                MessageBox.Show("Please provide your password.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                edxPassword.Focus();
                return false;
            }

            if (edxINI.Text == "")
            {
                MessageBox.Show("Please provide the path to Greenshot INI.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                edxINI.Focus();
                return false;
            }

            return true;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            //validate form
            if (validateForm())
            {
                string servername = edxJiraServer.Text;
                if (!edxJiraServer.Text.EndsWith("/"))
                {
                    servername += "/";
                }
                //assign the new settings
                Properties.Settings.Default.sJiraServer = servername;
                Properties.Settings.Default.sUsername = edxUsername.Text;
                Properties.Settings.Default.sPassword = edxPassword.Text;
                Properties.Settings.Default.sGreenshotINI = edxINI.Text;

                //save the settings
                Properties.Settings.Default.Save();

                MessageBox.Show("Your settings have been saved. Greenshot to Jira will close now.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);

                Close();
            }
        }

        private void btnIniBrowse_Click(object sender, EventArgs e)
        {
            dlgOpen.FileName = "Greenshot.ini";
            dlgOpen.Filter = "Ini Files (.ini)|*.ini";

            dlgOpen.FilterIndex = 0;
            dlgOpen.Multiselect = false;

            if (dlgOpen.ShowDialog() == DialogResult.OK)
            {
                edxINI.Text = dlgOpen.FileName;
            }
        }

        private void btnGreenShot_Click(object sender, EventArgs e)
        {
            if (edxINI.Text != "")
            {
                if (GREENSHOT_HANDLING.handleStatusinGreenShot(true, edxINI.Text))
                {
                    MessageBox.Show("You can now directly upload a screenshot to JIRA.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    btnGreenShot.Enabled = false;
                }
                else
                {
                    MessageBox.Show("An error occured while updating the ini file.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            else
            {
                MessageBox.Show("Please provide the Greenshot settings first.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
    }
}
